home *** CD-ROM | disk | FTP | other *** search
/ Download Now 8 / Download Now V8.iso / Program / InternetTools / ApacheWebServer1.3.6 / apache_1_3_6_win32.exe / _SETUP.1 / rotatelogs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-03  |  1.9 KB  |  83 lines

  1. /*
  2.  * Simple program to rotate Apache logs without having to kill the server.
  3.  *
  4.  * Contributed by Ben Laurie <ben@algroup.co.uk>
  5.  *
  6.  * 12 Mar 1996
  7.  */
  8.  
  9.  
  10. #define BUFSIZE        65536
  11. #define MAX_PATH    1024
  12.  
  13. #include "ap_config.h"
  14. #include <time.h>
  15. #include <errno.h>
  16. #include <fcntl.h>
  17.  
  18. int main (int argc, char **argv)
  19. {
  20.     char buf[BUFSIZE], buf2[MAX_PATH];
  21.     time_t tLogEnd = 0;
  22.     time_t tRotation;
  23.     int nLogFD = -1;
  24.     int nRead;
  25.     char *szLogRoot;
  26.  
  27.     if (argc != 3) {
  28.     fprintf(stderr,
  29.         "%s <logfile> <rotation time in seconds>\n\n",
  30.         argv[0]);
  31. #ifdef OS2
  32.     fprintf(stderr,
  33.         "Add this:\n\nTransferLog \"|%s.exe /some/where 86400\"\n\n",
  34.         argv[0]);
  35. #else
  36.     fprintf(stderr,
  37.         "Add this:\n\nTransferLog \"|%s /some/where 86400\"\n\n",
  38.         argv[0]);
  39. #endif
  40.     fprintf(stderr,
  41.         "to httpd.conf. The generated name will be /some/where.nnnn "
  42.         "where nnnn is the\nsystem time at which the log nominally "
  43.         "starts (N.B. this time will always be a\nmultiple of the "
  44.         "rotation time, so you can synchronize cron scripts with it).\n"
  45.         "At the end of each rotation time a new log is started.\n");
  46.     exit(1);
  47.     }
  48.  
  49.     szLogRoot = argv[1];
  50.     tRotation = atoi(argv[2]);
  51.     if (tRotation <= 0) {
  52.     fprintf(stderr, "Rotation time must be > 0\n");
  53.     exit(6);
  54.     }
  55.  
  56.     for (;;) {
  57.     nRead = read(0, buf, sizeof buf);
  58.     if (nRead == 0)
  59.         exit(3);
  60.     if (nRead < 0)
  61.         if (errno != EINTR)
  62.         exit(4);
  63.     if (nLogFD >= 0 && (time(NULL) >= tLogEnd || nRead < 0)) {
  64.         close(nLogFD);
  65.         nLogFD = -1;
  66.     }
  67.     if (nLogFD < 0) {
  68.         time_t tLogStart = (time(NULL) / tRotation) * tRotation;
  69.         sprintf(buf2, "%s.%010d", szLogRoot, (int) tLogStart);
  70.         tLogEnd = tLogStart + tRotation;
  71.         nLogFD = open(buf2, O_WRONLY | O_CREAT | O_APPEND, 0666);
  72.         if (nLogFD < 0) {
  73.         perror(buf2);
  74.         exit(2);
  75.         }
  76.     }
  77.     if (write(nLogFD, buf, nRead) != nRead) {
  78.         perror(buf2);
  79.         exit(5);
  80.     }
  81.     }
  82. }
  83.